package jee.boot.module.authority.service;

import jee.boot.common.basic.CrudService;
import jee.boot.entity.system.Filter;
import jee.boot.common.standard.AuthorityHandler;
import jee.boot.common.utils.StringUtils;
import jee.boot.module.authority.dao.GroupDao;
import jee.boot.module.authority.dao.SbjAggrDao;
import jee.boot.entity.auth.AuthConstant;
import jee.boot.entity.auth.Group;
import jee.boot.entity.auth.Role;
import jee.boot.entity.auth.SbjAggr;
import jee.boot.entity.system.BasicSbj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户组
 */
@Service
public class GroupService extends CrudService<GroupDao, Group> {

    @Autowired
    private SbjAggrDao sbjAggrDao;
    @Autowired
    RoleService roleService;

    @Override
    public Group get(Group entity) {
        entity = super.get(entity);
        getMany(entity);
        return entity;
    }

    /**
     * 补充一对多
     * @param entity
     */
    public void getMany(Group entity) {
        // 根据组ID查询所有角色
        List<SbjAggr> list = super.getOneToMany(sbjAggrDao, entity, new SbjAggr(), "sbj");
        if (list.size() > 0) {
            // 设置角色
            Role role = new Role();
            role.addFilter(new Filter("id", "in", getBasicObjIdListByType(AuthConstant.AUTH_SBJ_TYPE_ROLE, list)));
            entity.setRoleList(roleService.findList(role));

        }
    }

    @Override
    public void save(Group entity) {
        //保存数据
        super.save(entity);
        List<SbjAggr> list = new ArrayList<>();
        // 保存所有的角色关联
        if (entity.getRoleList() != null) {
            for (BasicSbj role : entity.getRoleList()) {
                list.add(new SbjAggr(AuthConstant.AUTH_SBJ_TYPE_GROUP, entity, AuthConstant.AUTH_SBJ_TYPE_ROLE, role));
            }
        }
        // 插入关联数据
        if (list.size() > 0) {
            super.saveOneToMany(sbjAggrDao, entity, "sbj", list);
        }
    }

    @Caching(evict = {
            @CacheEvict(value = AuthorityHandler.USER_AUTHORITY_KEY, allEntries = true)
    })
    @Override
    public void update(Group entity) {
        //更新数据
        super.update(entity);
        List<SbjAggr> list = new ArrayList<>();
        // 修改所有的角色关联
        if (entity.getRoleList() != null) {
            for (BasicSbj role : entity.getRoleList()) {
                list.add(new SbjAggr(AuthConstant.AUTH_SBJ_TYPE_GROUP, entity, AuthConstant.AUTH_SBJ_TYPE_ROLE, role));
            }
        }
        // 修改关联数据
        super.updateOneToMany(sbjAggrDao, entity, new SbjAggr(), "sbj", list);
    }

    @Caching(evict = {
            @CacheEvict(value = AuthorityHandler.USER_AUTHORITY_KEY, allEntries = true)
    })
    @Override
    public void delete(Group entity) {
        // 删除 组
        super.delete(entity);
        // 删除与role的关联关系
        super.deleteOneToMany(sbjAggrDao, entity, new SbjAggr(), "sbj");
    }

    /**
     * 根据关联表的数据按类型取出所有的权限主体entity的id
     *
     * @param type
     * @param list
     * @return
     */
    private List<String> getBasicObjIdListByType(String type, List<SbjAggr> list) {
        return list.stream()
                .filter(d -> StringUtils.equals(type, d.getObjType()))
                .map(d -> d.getObj().getId())
                .collect(Collectors.toList());
    }
}
